package com.itheima.contact.web;

import com.itheima.contact.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author ${Azer}
 * @LOVE,,,
 * @Date 2021/9/19 15:33
 */
@WebServlet("/deleteUserById")
public class DeleteUserById extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //从浏览器获取要删除的id
        int userid =Integer.parseInt(request.getParameter("id"));


//        System.out.println("用户id:"+userid);

        //实例化业务层
        UserService userService=new UserService();

        boolean result = userService.deleteUserById(userid);

        if (result){
            //删除成功
            response.sendRedirect("/delete_ok.jsp");
        }else {
            //删除失败
            response.getWriter().println("<h1>删除失败！3秒后跳转首页。。。</h1>");
            response.setHeader("refresh","3;url=//localhost:8080/index.jsp");
        }




    }
}
